package com.etc.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.etc.entity.Forum;
import com.etc.entity.Users;
import com.etc.entity.jsonui.JsonUi;
import com.etc.service.ForumService;
import com.etc.service.impl.ForumServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;


/**
 * Servlet implementation class CommunityServlet
 */
@WebServlet("/fs.do")
public class ForumServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ForumService forumService = new ForumServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ForumServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String op="query";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		
		
		if (op.equals("query")) {
			//查询数据库显示数据
			doForunQuery(request, response);
		}
		else if (op.equals("del")) {
			//删除数据
			//判断获取的articleId是否为空
			if (request.getParameter("articleId") != null) {
				//重新对编号进行赋值
				int id = Integer.parseInt(request.getParameter("articleId"));
				//把你获取到的标号传值给dao的删除方法
				boolean flag = forumService.DelForum(id);
				
				PrintWriter out = response.getWriter();
				out.print(flag);
				out.close();
				
			}
		}else if (op.equals("update")) {
			//修改操作
			//获取页面另一侧的ajax请求的json数据
			BufferedReader reader = request.getReader();
			StringBuffer uBuffer = new StringBuffer();
			String str = null;
			while ((str = reader.readLine()) != null) {
				uBuffer.append(str);
			}
			Gson gson = new Gson();
			Forum forum = gson.fromJson(uBuffer+"", Forum.class);
			boolean flag = forumService.UpdaForum(forum);
			PrintWriter out = response.getWriter();
			out.print(flag);
			out.close();
		}
		else if(op.equals("removesession")) {
			//退出后清除所有的session信息
			request.getSession().invalidate();
			request.getRequestDispatcher("index.jsp").forward(request, response);

		}
		
		
	}

	private void doForunQuery(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		int page = 1;
		if (request.getParameter("page") != null) {
			page =Integer.parseInt(request.getParameter("page"));
		}
		
		int limit = 10;
		if (request.getParameter("limit") != null) {
			limit =Integer.parseInt(request.getParameter("limit"));
		}
		
		String strLike = "";
		if (request.getParameter("strLike") != null) {
			strLike =request.getParameter("strLike");
		}
		PageData<Forum> pd = forumService.GetForumAll(page, limit, strLike);
	    
		
		JsonUi<Forum> jsonUi = new JsonUi<>(0,"",pd.getTotalCount(),pd.getData());
		
		Gson gson = new Gson();
		String string = gson.toJson(jsonUi);
		
		out.print(string);
		
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
